<?php $value = $this->model->{$this->attribute}; ?>
<div class="dropdown">
    <?php if ($value != ""): ?>
        <a class="dropdown-toggle btn" role="button" data-toggle="dropdown" data-target="#">
            <?php echo $this->label; ?>
            <b class="caret"></b>
        </a>
        <span class="hint"><?php echo Yii::t('main', '(click label on the left for more actions)') ?></span>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
            <li><a href="#" id="<?php echo $this->attribute ?>-delete">Delete</a></li>
            <li><a href="<?php echo Yii::app()->baseUrl . '/' . $this->uploadPath . '/' . $value; ?>" target="_blank">Download</a></li>
        </ul>
    <?php else: ?>
        <label for="<?php echo get_class($this->model) . '_' . $this->attribute ?>"><?php echo $this->label; ?></label>
    <?php endif; ?>
</div>

<span id="<?php echo $this->attribute ?>-filepath">
    <?php
    if (!empty($value) && in_array(strtolower(pathinfo($value, PATHINFO_EXTENSION)), array('gif', 'jpg', 'png', 'jpeg')))
    {
        $image = Yii::app()->params['hotelImgUploadDir'] . 'cover_photo/' . $value;
        //echo CHtml::image($image, $value, $this->imageHtmlOptions);
        echo CHtml::image(Yii::app()->baseUrl . '/image.php?thumb=' . FlexImage::createThumbFilename($image, 140, 100), $value);
        echo CHtml::image(Yii::app()->baseUrl . '/image.php?thumb=' . FlexImage::createThumbFilename($image, 620, 400), $value, array('style' => 'display:none'));
        echo CHtml::image(Yii::app()->baseUrl . '/image.php?thumb=' . FlexImage::createThumbFilename($image, 270, 190), $value, array('style' => 'display:none'));
    }
    elseif (!empty($value))
        echo "{$value}";
    else
    {
        //echo CHtml::image(Yii::app()->theme->baseUrl.'/images/no_images.jpg','',$this->imageHtmlOptions);
    }
    ?>
</span>

<?php
$livePath = Yii::app()->baseUrl . "/files/sweeUpload/" . Yii::app()->getSession()->getSessionId() . "/" . get_class($this->model) . "_" . $this->attribute . "/";
echo Sweeml::activeAsyncFileUpload($this->model, $this->attribute, array(
    'config' => array(
        'runtimes'       => 'html5, flash',
        'auto'           => true,
        'ui'             => true,
        'maxFileSize'    => '2mb',
        'multiSelection' => $this->multiSelection,
    ),
    'events' => array(
        'beforeUpload' => 'js:function(up, file){
            $("#submitButton").attr("disabled", "disabled");
        }',
        'fileUploaded' => "js:function (up, file, response) {
    var json = jQuery.parseJSON(response.response);
    if(json.status == false) {
        jQuery('#'+up.getListId()+' #'+file.id).fadeOut('slow', function(){ jQuery(this).remove(); });
    } else {
        if(up.getMultiSelection()==false) {
            jQuery('#'+up.getListId()+' .fileContainer').each(function(idx, el){
                if($(el).attr('id') != file.id) {
                    $(el).remove();
                }
            });
        }
					jQuery('#'+up.getListId()+' #'+file.id+' div.progress').css({width:'100%'});
					jQuery('#'+up.getListId()+' #'+file.id).prepend('<a href=\"javascript:void(0);\" onClick=\"jQuery.ajax.asyncDelete(\''+up.getId()+'\', \''+file.id+'\', function(id){ jQuery(\'#\'+id).remove();});\">X</a>');
					var imgSrc='{$livePath}'+json.fileName;
					jQuery('#'+up.getListId()+' #'+file.id).prepend('<span><img width=\"120px\" src=\"'+imgSrc+'\" alt=\"'+file.name+'\"></span>');
				}
}",
//        'uploadComplete' => 'js:function(up, files){
//             var filesJson=files.pop();
//            var filesName=filesJson.name;
//            var ext = filesName.substr(filesName.lastIndexOf(".") + 1);
//            var container = $("#'.$this->attribute.'-filepath");
//            console.log(container);
//            if(ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "gif"){
//                container.html(\''.CHtml::image('#','',$this->imageHtmlOptions).'\');
//                container.find("img").attr(
//                    "src",
//                    "'.Yii::app()->baseUrl.'/uploads/sweeUpload/'.Yii::app()->getSession()->getSessionId().'/'.get_class($model).'_'.$this->attribute.'/"+filesJson.name);
//            }
//        }',
    )
));
?>

<?php
$script   = "\$('#{$this->attribute}-delete').click(function(){
        var inputContainer = \$('div#" . get_class($this->model) . "_{$this->attribute}_hidden');
        if (inputContainer.find('input').length == 0)
        {
            inputContainer.append('<input name=\"" . get_class($this->model) . "[{$this->attribute}]\" value=\"\" />');
        }
        else
            inputContainer.find('input').val('');
            
        \$('span#{$this->attribute}-filepath').html('Removed');
    })";
Yii::app()->clientScript->registerScript($this->attribute . '-delete', $script, CClientScript::POS_END);
?>